/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmRepresentantesXPartido.java
 *
 * Created on 16-jun-2011, 7:28:15
 */
package verins.GUI;

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import verins.beans.Personarnv;
import verins.beans.Procesoelectoral;
import javax.swing.JDialog;
import javax.swing.table.DefaultTableModel;
import verins.BO.PartidopoliticoBO;
import verins.BO.PersonarnvBO;
import verins.BO.RpteXPartidoBO;
import verins.beans.Partidopolitico;
import verins.beans.Persona;
import verins.beans.Representantesxproceso;
import verins.beans.RepresentantesxprocesoPK;
/**
 *
 * @author yi
 */
public class frmRepresentantesXPartido extends JDialog {
    
     private Procesoelectoral procesoElectoral =null;
     private Partidopolitico partido =null;
     public Personarnv personaBusqueda=null;
    
     private List<Representantesxproceso> representantes=null;
     
     private List lstRepPersonas=new ArrayList();
  
    /** Creates new form frmRepresentantesXPartido */
     public frmRepresentantesXPartido(Procesoelectoral proceso,Partidopolitico pa, JDialog ventana) {
        super(ventana,true);
        procesoElectoral = proceso;
        partido= pa;
        
        initComponents();
        
        representantes=RpteXPartidoBO.findAll(partido.getIdpartidopolitico(), procesoElectoral.getIdprocesoelectoral());
        
        
        llenarTabla();
      
        
        if ((procesoElectoral != null) && (partido!=null)) {
            
            
            MostrarDatos(procesoElectoral,partido);
             
            
        }
        
         
      
        
        this.setSize(new Dimension(1080, 550));
        verins.Utils.Comunes.centrarVentana(this);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblProcesoElectoral = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblNombreProceso = new javax.swing.JLabel();
        lblNombrePartido = new javax.swing.JLabel();
        lblTCodigoProceso = new javax.swing.JLabel();
        lblCodProceso = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPersonas = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnDrop = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Representantes por Partido");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Proceso Electoral"));

        lblProcesoElectoral.setText("Proceso Electoral");

        jLabel1.setText("Partido Político");

        lblNombreProceso.setText(":");

        lblNombrePartido.setText(":");

        lblTCodigoProceso.setText("Código Proceso Electoral");

        lblCodProceso.setText(":");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblProcesoElectoral, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(67, 67, 67)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblNombreProceso, javax.swing.GroupLayout.PREFERRED_SIZE, 448, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(lblTCodigoProceso))
                    .addComponent(lblNombrePartido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(lblCodProceso, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProcesoElectoral)
                    .addComponent(lblNombreProceso)
                    .addComponent(lblTCodigoProceso)
                    .addComponent(lblCodProceso, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblNombrePartido))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblPersonas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Dni", "Ap. Paterno", "Ap. Materno", "Nombres"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPersonas.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblPersonas);
        tblPersonas.getColumnModel().getColumn(0).setResizable(false);
        tblPersonas.getColumnModel().getColumn(1).setResizable(false);
        tblPersonas.getColumnModel().getColumn(2).setResizable(false);
        tblPersonas.getColumnModel().getColumn(3).setResizable(false);

        btnAdd.setFont(new java.awt.Font("Ubuntu", 1, 10));
        btnAdd.setText("+");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDrop.setFont(new java.awt.Font("Ubuntu", 1, 10));
        btnDrop.setText("-");
        btnDrop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDropActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, 0, 988, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 931, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancelar)
                                .addGap(2, 2, 2)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnDrop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(btnDrop, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    
    public void llenarTabla(){
        
       
       
        verins.Utils.Comunes.limpiarTabla(tblPersonas);
        DefaultTableModel dtm=(DefaultTableModel)tblPersonas.getModel();
        
        if (!representantes.isEmpty())
            lstRepPersonas=representantes;
        if (lstRepPersonas!=null&& lstRepPersonas.size()>0){
            Iterator it=lstRepPersonas.iterator();
            
            
            Personarnv p=null;
            String dni="";
            while(it.hasNext()){
              // String dni, int idprocesoelectoral, int idpartidopolitico
                dni=((Representantesxproceso)it.next()).getRepresentantesxprocesoPK().getDni();
                p=PersonarnvBO.findByDNI(dni);
               
                Object fila[]=new Object[] {p.getDni(),
                                            p.getApepaterno(),
                                            p.getApematerno(),p.getNombres()
                                            };
                dtm.addRow(fila);
            }
        }
        verins.Utils.Comunes.setearColumnas(tblPersonas);
    }
    
    
    
    
    
    
    
    
    
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
         if (verins.Utils.Mensajes.guardar(evt, this)){
                if (verins.Utils.Mensajes.exito(RpteXPartidoBO.guardarRepresentantes(lstRepPersonas)))
                dispose();
            }
        
        
        
        
        
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        
         frmBusquedaPersona frmBusq=new frmBusquedaPersona(this);
         frmBusq.mostrarBotonAgregar(true);
         frmBusq.setVisible(true);
        personaBusqueda=(Personarnv)frmBusq.personaBusqueda;
        
        if (personaBusqueda!=null){
            
           DefaultTableModel dtm=(DefaultTableModel)tblPersonas.getModel();
           Object fila[]=new Object[] {personaBusqueda.getDni(),personaBusqueda.getApepaterno(),personaBusqueda.getApematerno(),personaBusqueda.getNombres()};
           dtm.addRow(fila);
           
           //lista para grabar
           // Representantesxproceso(String dni, int idprocesoelectoral, int idpartidopolitico
           // String dni, int idprocesoelectoral, int idpartidopolitico
           //Representantesxproceso rxp=new Representantesxproceso(personaBusqueda.getDni(),procesoElectoral.getIdprocesoelectoral(),partido.getIdpartidopolitico());
           Representantesxproceso rxp=new Representantesxproceso();
             rxp.setRepresentantesxprocesoPK(new RepresentantesxprocesoPK( personaBusqueda.getDni(),procesoElectoral.getIdprocesoelectoral(),partido.getIdpartidopolitico()));
            System.out.println(procesoElectoral.getNombre());
                System.out.println(partido.getNombre());
           lstRepPersonas.add(rxp);
          // partido=null;<<z
           //procesoElectoral=null;
          if ( partido.getPersonaList()!=null){
             partido.getPersonaList().add(partido.getPersonaList().size(), new Persona(personaBusqueda.getDni()));
          }
        }
        
    }//GEN-LAST:event_btnAddActionPerformed

    
    
     private void MostrarDatos(Procesoelectoral proceso, Partidopolitico part) {

       // lblCodigoProceso.setText(proceso.getIdprocesoelectoral() + " ");
        lblCodProceso.setText(lblCodProceso.getText()+" "+proceso.getIdprocesoelectoral());
        lblNombreProceso.setText(proceso.getNombre().toUpperCase());
        lblNombrePartido.setText(lblNombrePartido.getText() +" "+partido.getNombre());
        System.out.println(proceso.getNombre());
    }
    
    
    
    
    
    
    
    
    private void btnDropActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDropActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnDropActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        
         dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmRepresentantesXPartido(null,null,null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnDrop;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCodProceso;
    private javax.swing.JLabel lblNombrePartido;
    private javax.swing.JLabel lblNombreProceso;
    private javax.swing.JLabel lblProcesoElectoral;
    private javax.swing.JLabel lblTCodigoProceso;
    private javax.swing.JTable tblPersonas;
    // End of variables declaration//GEN-END:variables
}
